package org.example.mall.admin.controller.marketing;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.example.mall.admin.common.es.EsPageUtil;
import org.example.mall.admin.configuration.resource.Inner;
import org.example.mall.admin.model.entity.marketing.Article;
import org.example.mall.admin.service.marketing.ArticleService;
import org.example.mall.admin.util.BusOp;
import org.example.mall.admin.util.R;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * <p>
 * 文章管理 前端控制器
 * </p>
 *
 * @author hzq
 * @since 2022-01-12
 */
@Api(tags = "文章管理")
@RestController
@RequestMapping("/marketing/article")
@AllArgsConstructor
public class ArticleController {

    private final ArticleService articleService;

    @GetMapping("/page")
    @ApiOperation("分页查询文章列表")
    public R<IPage<Article>> page(Page page, Article article) {
        return R.ofSuccess(articleService.getPage(page, article));
    }

    @PostMapping
    @ApiOperation("添加文章")
    @PreAuthorize("hasAuthority('article_add')")
    public R<Boolean> add(@RequestBody @Valid Article article) {
        return R.ofSuccess("添加文章成功",
                BusOp.mustSucceed(articleService.add(article), "添加文章失败"));
    }

    @PutMapping
    @ApiOperation("编辑文章")
    @PreAuthorize("hasAuthority('article_edit')")
    public R<Boolean> edit(@RequestBody @Valid Article article) {
        return R.ofSuccess("编辑文章成功",
                BusOp.mustSucceed(articleService.edit(article), "编辑文章失败"));
    }

    @DeleteMapping("/{articleId}")
    @ApiOperation("删除文章")
    @PreAuthorize("hasAuthority('article_del')")
    public R<Boolean> del(@PathVariable Integer articleId) {
        return R.ofSuccess("删除文章成功",
                BusOp.mustSucceed(articleService.del(articleId), "删除文章失败"));
    }

    @Inner(false)
    @GetMapping("/esPage")
    @ApiOperation("ES引擎(标题、作者、标签、简介)")
    public R<IPage<Map<String, Object>>> esPage(Page page, @RequestParam(required = false) String text) {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 查询条件
        if (StrUtil.isNotBlank(text)) {
            BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
//            boolQuery.must(QueryBuilders.matchQuery("title", text));
            // 与以下任意字段配置
            boolQuery.should(QueryBuilders.multiMatchQuery(text, "title", "author", "label", "introduction"));
            searchSourceBuilder.query(boolQuery);
        }
        // 查询全部
        else {
            searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        }
        // 排除内容字段
        // searchSourceBuilder.fetchSource(null, null);
        // 高亮字段
        EsPageUtil.setHighlightField(searchSourceBuilder, "title", "author", "label", "introduction");
        // 设置分页
        EsPageUtil.setPageParams(searchSourceBuilder, page);
        // 搜索
        SearchResponse response = articleService.search(searchSourceBuilder);
        // 设置分页结果
        EsPageUtil.setPageResultMap(response, page);
        return R.ofSuccess(page);
    }
}

